<template>
  <div class="app-container">
    <el-form
    ref="listFilterRef"
    :model="list.filter"
    inline
    class="list-filter"
    > 
      <el-form-item prop="productName">
        <el-input v-model="list.filter.productName" placeholder="输入商品名称搜索" clearable>
          <i slot="prefix" class="el-input__icon el-icon-search"/>
        </el-input>
      </el-form-item>

      <el-form-item prop="likeOrderId">
        <el-input v-model="list.filter.likeOrderId" placeholder="订单号模糊匹配" clearable>
          <i slot="prefix" class="el-input__icon el-icon-search"/>
        </el-input>
      </el-form-item>

      <el-form-item prop="status">
        <KiSelect
          :index.sync="list.filter.status"
          :options="typeCollector.statusOfOrder"
          placeholder="状态"
        />
      </el-form-item>

      <el-form-item prop="regEndTime">
        <TimeRangePicker
          :start.sync="list.filter.startTime"
          :end.sync="list.filter.endTime"
          placeholder="订单创建开始时间/订单创建结束时间"
        />
      </el-form-item>
      <el-button @click="reset">重置</el-button>
    </el-form>

    <div class="table-operation">
      <ExportExcel
        url="/sot-admin-api/vip/vip-order-web/downloadList"
        :param="list.filter"
        :disabled="!(list.data && list.data.length > 0)"
      />
      <el-pagination
        v-bind="elPaginationProps"
        :total="list.total"
        :currentPage.sync="list.filter.pageNo"
        :pageSize.sync="list.filter.pageSize"
      />
    </div>

    <el-table
      stripe
      v-loading="list.loading"
      :data="list.data"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" type="index"/>
      <el-table-column label="订单号">
        <template slot-scope="{row}">
          {{ row.order.orderId }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称">
        <template slot-scope="{row}">
          {{ row.order.productName }}
        </template>
      </el-table-column>
      <el-table-column label="商品数量">
        <template slot-scope="{row}">
          {{ row.order.quantity }}
        </template>
      </el-table-column>
      <el-table-column label="积分">
        <template slot-scope="{row}">
          {{ row.order.integral }}
        </template>
      </el-table-column>
      <el-table-column label="收货人信息">
        <template slot-scope="{row}">
          <div>{{ row.receiptInfo.name }}</div>
          <div>{{ row.receiptInfo.phone }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{row}">
          {{ row.order.statusName }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="{row}">
          {{ row.order.orderTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <AuthButton @click="r({
                orderId: row.order.orderId
              })"  name="查看"/>

          <AuthButton 
           v-if="row.order.status == 16"
           @click="handleRow(row).then(showOfDeliveryDialog = true)" 
           circle 
           type="success"  
           name="去发货">
            <i class="el-icon-s-promotion"/>
          </AuthButton>

          <AuthButton 
           v-if="[16, 17, 18].includes(row.order.status)"
           @click="handleRow(row).then(showOfIntegralDialog = true)" 
           circle 
           type="danger" 
           name="退还积分">
            <i class="el-icon-s-fold"/>
          </AuthButton>
          <AuthButton 
          v-if="row.order.status == 17"
          @click="handleRow(row).then(showOfLogisticsDialog = true)" 
          circle 
          type="warning"  
          name="物流信息">
            <i class="el-icon-truck"/>
          </AuthButton>
        </template>
      </el-table-column>
    </el-table>

    <KiFormDialog
      :show.sync="form.show"
      title="订单详情"
      v-model="form.data"
      :submit="submitForm"
      :readonly="form.status === 'r'"
      ref="formRef"
    >
      <!-- <template #el-form> -->
        <template v-if="detailsOfRow.order">
          <h3 class="subtitle"><span>商品信息</span></h3>
          <div class="flex justify-between">
            <UnivariateTable>
              <tr>
                <td>商品编码</td>
                <td>{{ detailsOfRow.order.productId }}</td>
                <td>商品名称</td>
                <td>{{ detailsOfRow.order.productName }}</td>
              </tr>
              <tr>
                <td>兑换积分</td>
                <td>{{ $operation(detailsOfRow.order.integral, '/', detailsOfRow.order.quantity ) }}</td>
              </tr>
            </UnivariateTable>
          </div>
        </template>
        <template v-if="detailsOfRow.order">
          <el-divider/>
          <h3 class="subtitle"><span>订单信息</span></h3>
          <div class="flex justify-between">
            <UnivariateTable>
              <tr>
                <td>订单编号</td>
                <td>{{ detailsOfRow.order.orderId }}</td>
                <td>数量</td>
                <td>{{ detailsOfRow.order.quantity }}</td>
              </tr>
              <tr>
                <td>实付积分</td>
                <td>{{ detailsOfRow.order.integral }}</td>
                <td>订单状态</td>
                <td>{{ detailsOfRow.order.statusName }}</td>
              </tr>
              <tr>
                <td>订单备注</td>
                <td>{{ detailsOfRow.order.remark||'暂无备注信息！'}}</td>
              </tr>
            </UnivariateTable>
          </div>
        </template>
        <template v-if="detailsOfRow.receiptInfo">
          <el-divider/>
          <h3 class="subtitle"><span>收货信息</span></h3>
          <div class="flex justify-between">
            <UnivariateTable>
              <tr>
                <td>姓名</td>
                <td>{{ detailsOfRow.receiptInfo.name }}</td>
                <td>电话</td>
                <td>{{ detailsOfRow.receiptInfo.phone }}</td>
              </tr>
              <tr>
                <td>地址</td>
                <td>{{ detailsOfRow.receiptInfo.areaName + detailsOfRow.receiptInfo.address}}</td>
              </tr>
            </UnivariateTable>
          </div>
        </template>
        <template  v-if="detailsOfRow.orderChangeList">
          <el-divider/>
          <h3 class="subtitle"><span>变更信息</span></h3>
          <el-table
            stripe
            v-loading="form.loading"
            :data="detailsOfRow.orderChangeList"
            border
            fit
            highlight-current-row
          >
            <el-table-column align="center" type="index"/>
            <el-table-column label="变更状态" prop="statusName"/>
            <el-table-column label="变更时间" prop="changeTime"/>
          </el-table>
        </template>
      <!-- </template> -->
    </KiFormDialog>

    <DeliveryDialog 
    :show.sync="showOfDeliveryDialog" 
    :parentRowOrderId="orderId" 
    :expressCompany="expressCompany" 
    @refresh="getList()" />

    <IntegralDialog 
    :show.sync="showOfIntegralDialog"
    :parentRowOrderId="orderId"
    @refresh="getList()" />
    
    <LogisticsDialog 
    :show.sync="showOfLogisticsDialog"
    :parentRowOrderId="orderId" 
    :expressCompany="expressCompany"
    @refresh="getList()" />

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import pageMixin from '@/utils/pageMixin'

import DeliveryDialog from './components/DeliveryDialog'
import IntegralDialog from './components/IntegralDialog'
import LogisticsDialog from './components/LogisticsDialog'

export default {
  mixins: [pageMixin],
  components: { DeliveryDialog, IntegralDialog, LogisticsDialog },

  data() {
    return {
      orderId: '',
      detailsOfRow: {},
      expressCompany: [],
      loading: false,
      showOfDeliveryDialog: false,
      showOfIntegralDialog: false,
      showOfLogisticsDialog: false
    }
  },
  computed:{
    ...mapGetters([
      'typeCollector'
    ]),
  },
  setup: () =>
    useAdmateAdapter(
      {
      urlPrefix: '/sot-admin-api',
      axiosConfig: {
        getList: {
          url: 'vip/vip-order-web/queryOrderForPage'
        },
        r: {
          url: 'vip/vip-order-web/queryOrderDetail',
        },
      },
      form: {
        data: {
          status: 1
        }
      }
    },
    {
      afterOpenForm(e){
        this.detailsOfRow = e.data
      }
    }
  ),
  mounted() {
    this.$POST('sot-admin-api/fundamental/express/queryExpressCompany').then( _ => {
      this.expressCompany = _.data
    })
  },
  methods: {
    async handleRow(row) {
      this.orderId = row?.receiptInfo?.orderId
    },
    exportList () {
      this.loading = true
      this.$download('sot-admin-api/vip/vip-order-web/downloadList', this.list.filter, { method: 'POST' }).finally(e => {
        this.loading = false
      })
    },
  }
}
</script>